package com.gxp.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.gxp.pojo.User;

public class UserValidator implements Validator {

	@Override
	public boolean supports(Class<?> clazz) {
		return User.class.equals(clazz);
	}

	@Override
	public void validate(Object obj, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userId", null, "Username is empty.");  
	       User user = (User) obj;  
	       if (null == user.getPassword() || "".equals(user.getPassword())) {
	    	   errors.rejectValue("password", null, "Password is empty.");  
	       }
	}

}



/*private final Validator addressValidator;

public CustomerValidator(Validator addressValidator) {
   if (addressValidator == null) {
       throw new IllegalArgumentException("The supplied [Validator] is required and must not be null.");
   }
   if (!addressValidator.supports(Address.class)) {
       throw new IllegalArgumentException(
         "The supplied [Validator] must support the validation of [Address] instances.");
   }
   this.addressValidator = addressValidator;
}

 *//**
 * This Validator validates Customer instances, and any subclasses of Customer too
 *//*
public boolean supports(Class clazz) {
   return Customer.class.isAssignableFrom(clazz);
}

public void validate(Object target, Errors errors) {
   ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "field.required");
   ValidationUtils.rejectIfEmptyOrWhitespace(errors, "surname", "field.required");
   Customer customer = (Customer) target;
   try {
       errors.pushNestedPath("address");
       ValidationUtils.invokeValidator(this.addressValidator, customer.getAddress(), errors);
   } finally {
       errors.popNestedPath();
   }
}*/
